package controllers

import (
	"beegoProject/models"
	"fmt"
	"github.com/beego/beego/v2/client/orm"
	beego "github.com/beego/beego/v2/server/web"
	"math"
	"strconv"
	"time"
)

type MainController struct {
	beego.Controller
}
type CountController struct {
	beego.Controller
}
type IndexController struct {
	beego.Controller
}
type GetUserList struct {
	beego.Controller
}
type DeleteUser struct {
	beego.Controller
}
type AddUser struct {
	beego.Controller
}
type RegisterMain struct {
	beego.Controller
}
type LoginMain struct {
	beego.Controller
}
type AddArticle struct {
	beego.Controller
}
type GetArticleList struct {
	beego.Controller
}
type DeleteArticle struct {
	beego.Controller
}

func (c *MainController) Get() {
	c.Data["Website"] = "1209322734.com"
	c.Data["Email"] = "1209322734@demo.com"

	id := c.Ctx.Input.Param(":id")

	// 调用model，查询用户id为1 的用户	信息

	zhuanhuahoudeId, _ := strconv.Atoi(id)
	user := models.GetUserById(zhuanhuahoudeId)
	// 然后将user数据保存到Data中, 将参数传给后面的views视图模板处理

	c.Controller.Data["json"] = user
	c.ServeJSON()
	fmt.Println(user)
	//c.Data["user"] = user
	// 使用新的视图模板user.tpl
	//c.TplName = "user.tpl"
}

func (c *CountController) Get() {
	c.Data["Website"] = "1209322734.com"
	c.Data["Email"] = "1209322734@demo.com"
	fmt.Println(1)
	count := models.GetCount()
	c.Controller.Data["json"] = count
	c.ServeJSON()
	//c.TplName = "user.tpl"
}
func (c *IndexController) Post() {
	c.Data["test"] = "区块链"
	//c.TplName = "test.html"
}
func (c *GetUserList) Get() {
	UserList := models.GetUserArr()
	c.Controller.Data["json"] = UserList
	c.ServeJSON()
	//c.TplName = "user.tpl"
}
func (c *DeleteUser) Delete() {
	id := c.Ctx.Input.Param(":id")
	user := models.DeleteUser(id)
	c.Controller.Data["json"] = user
	c.ServeJSON()
	//c.TplName = "user.tpl"
}
func (c *AddUser) Post() {
	username := c.Ctx.Input.Param(":username")
	password := c.Ctx.Input.Param(":password")

	user := models.AddUser(username, password)
	c.Controller.Data["json"] = user
	c.ServeJSON()
	//c.TplName = "user.tpl"
}
func (c *RegisterMain) PostRegister() {
	userName := c.GetStrings("userName")
	password := c.GetStrings("password")
	o := orm.NewOrm()
	user := models.User{}
	fmt.Println(userName)
	fmt.Println(password)

	user.Username = userName[0]
	user.Password = password[0]
	_, err := o.Insert(&user)
	if err != nil {
		fmt.Println(err)
		c.Redirect("/register", 302)
	}
	//c.Ctx.WriteString("注册成功")
	c.TplName = "login.html"
}
func (c *RegisterMain) GetRegister() {

	c.TplName = "register.html"
}

func (c *LoginMain) GetLogin() {
	name := c.Ctx.GetCookie("userName")
	if name == "" {

	}
	c.Data["userName"] = name
	c.Data["data"] = "aa"
	c.TplName = "login.html"
}

func (c *LoginMain) PostLogin() {
	userName := c.GetStrings("userName")
	password := c.GetStrings("password")

	//fmt.Println(userName)
	//fmt.Println(password)
	o := orm.NewOrm()
	user := models.User{}
	user.Username = userName[0]
	user.Password = password[0]

	err := o.Read(&user, "username", "password")
	if err != nil {
		fmt.Println(err)
		c.Redirect("/login", 302)
	}
	check := c.GetStrings("remember")
	fmt.Print(check)
	//if check[0] == "on" {
	//	c.Ctx.SetCookie("userName", user.Username, time.Second*100)
	//} else {
	//	c.Ctx.SetCookie("userName", "sss", -1)
	//}
	c.Data["userName"] = userName
	c.Ctx.SetCookie("userName", user.Username, time.Second*100)
	//c.Redirect("/addArticle", 302)
	c.Redirect("/Article/GetArticleList", 302)
}
func (c *AddArticle) PostArticle() {
	// 1 、拿到数据
	//fmt.Print(12)
	articleName := c.GetStrings("articleName")
	content := c.GetStrings("content")
	file, header, err := c.GetFile("uploadname")
	defer file.Close()
	if err != nil {
		fmt.Print("上传失败")
		return
	} else {
		c.SaveToFile("uploadname", "./static/img/"+header.Filename)
	}
	//fmt.Print(articleName)
	//fmt.Print(content)
	//2 、判断数据是否为空
	if articleName[0] == "" || content[0] == "" {
		fmt.Print("添加文章数据为空")
	}
	//3、插入数据
	o := orm.NewOrm()
	article := models.Article{}
	article.Title = content[0]
	//article.Id = 4
	article.Content = articleName[0]
	article.Img = "./static/img/" + header.Filename
	_, err = o.Insert(&article)
	if err != nil {
		fmt.Print("有错误")
	}
	//c.Ctx.WriteString("操作成功")
	c.Redirect("/Article/GetArticleList", 302)
}

/*
点击发起 GET 添加文章
*/
func (c *AddArticle) GetArticle() {
	c.TplName = "add.html"
}

/*
查询文章列表
*/
func (c *GetArticleList) Get() {
	articles := models.GetArticleList()

	//c.Controller.Data["json"] = articles
	pageIndex, err := c.GetInt("pageIndex")
	if err != nil {
		//若未获取到页码，设置默认页码1
		pageIndex = 1
	}
	fmt.Print(pageIndex)
	o := orm.NewOrm()
	table := o.QueryTable("article")
	count, _ := table.Count()
	//fmt.Print(count)
	c.Data["count"] = count
	//设置每页的数据条数
	pageSetNum := 3
	// 总页数
	pageCount := math.Ceil((float64(count) / float64(pageSetNum)))
	//当前第几页
	enablelast, enablenext := true, true
	if pageIndex == 1 {
		enablelast = false
	}
	if pageIndex == int(pageCount) {
		enablenext = false
	}
	c.Data["EnableNext"] = enablenext
	c.Data["EnableLast"] = enablelast
	c.Data["pageCount"] = pageCount
	start := pageSetNum * (pageIndex - 1)
	table.Limit(pageSetNum, start).All(&articles)
	c.Data["articles"] = articles
	c.Data["pageIndex"] = pageIndex
	c.TplName = "articleList.html"
}
func (c *DeleteArticle) Get() {
	id := c.GetStrings("id")
	fmt.Print(id)
	articles := models.DeleteArticle(id)
	c.Data["articles"] = articles
	c.Redirect("/Article/GetArticleList", 302)
}
